package com.ruoyi.cpsp.service.impl;/*
    @author neu.xiangwenjuan
    @create 2024/5/25
*/

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.cpsp.domain.Department;
import com.ruoyi.cpsp.domain.SysUserInfo;
import com.ruoyi.cpsp.mapper.*;
import com.ruoyi.cpsp.service.DepartmentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentUserServiceImpl implements DepartmentUserService {

    @Autowired
    private SysUserInfoMapper userMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private TeamGroupMapper teamGroupMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private CurrentPerformanceDataMapper dataMapper;


    @Override
    public List<SysUserInfo> getAllDepartmentUserInfo() {
        // 获取到所有的，然后再筛选吧
        // 还需要做一个筛选，筛选出
        List<SysUserInfo> userInfos = userMapper.selectUserList();
        List<SysUserInfo> results = new ArrayList<>();
        for (SysUserInfo userInfo : userInfos) {
            if (userInfo.getUserId() >= 101) {
                results.add(userInfo);
            }
        }
        return results;
    }

    @Override
    public int deleteDepartmentUser(Integer userId, Integer deptId) {
        SysDept sysDept = userMapper.selectDeptById(deptId);
        String departmentId = sysDept.getDepartmentId();
        // 还需要根据departmentId删除班组、该部门下的员工，和该部门下员工的当前绩效的数据。
        try {
            Integer tag = departmentMapper.deleteByDepartmentId(departmentId);
            // 删除班组
            Integer team_tag = teamGroupMapper.deleteTeamGroupByDepart(departmentId);
            // 删除员工，更新状态为2
            List<String> idCards = employeeMapper.selectCardByDepart(departmentId, null);
            int employ_tag = employeeMapper.deleteByDepart(departmentId, 2);
            // 删除当前季度绩效
            for (String idcard : idCards) {
                dataMapper.deleteCurrentByCard(idcard);
            }
            // 删除dept表中一行数据，根据deptId
            int del_tag = userMapper.deleteDeptById((long)deptId);
            // 删除sys_user表中一行数据，根据user_id
            int user_del = userMapper.deleteUserById((long) userId);
            // 删除sys_user_role表中一行数据，根据user_id
            int user_role_del = userMapper.deleteUserRoleByUserId((long) userId);
            if (tag > 0 && del_tag >0 && user_del > 0 && user_role_del > 0) {
                return 1;
            }else {
                return 0;
            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("删除部门失败！");
            return 0;
        }
    }

    @Override
    public int insertDepartmentUser(String departmentName, String departId, String username, String password, String tel, String email) {
        try {
            // 第一步，插入到department表，需要departmentName和departId
            Department department = new Department();
            department.setDepartmentId(departId);
            department.setDepartmentName(departmentName);
            int dept = departmentMapper.insert(department);
            // 第二步，插入到sys_dept表，需要较多信息
            SysDept sysDept = new SysDept();
            sysDept.setParentId((long) 100);
            sysDept.setDeptName(departmentName);
            sysDept.setAncestors("0,100");
            // 查找最大值
            sysDept.setOrderNum(userMapper.selectMaxOrderNum());
            sysDept.setLeader("");
            sysDept.setPhone(tel);
            sysDept.setEmail(email);
            sysDept.setStatus("0");
            sysDept.setCreateBy("admin");
            sysDept.setDepartmentId(departId);
            int dept_tag = userMapper.insertDept(sysDept);
            // 第三步，插入到sys_user表，需要一些信息，可以复用一些时间...
            SysUser sysUser = new SysUser();
            // 查找dept_id值
            sysUser.setDeptId(userMapper.selectDeptId());
            sysUser.setUserName(username);
            sysUser.setNickName(departmentName);
            sysUser.setEmail(email);
            sysUser.setAvatar("");
            sysUser.setPhonenumber(tel);
            sysUser.setSex("0");
            sysUser.setPassword(password);
            sysUser.setStatus("0");
            sysUser.setCreateBy("admin");
            int user_tag = userMapper.insertUser(sysUser);
            // 第四步，插入到sys_user_role表，需要role_id,user_id
            // 从数据库中查询
            long userId = (long) userMapper.selectUserId();
            long roleId = 2;
            int role_tag = userMapper.insertUserRole(userId, roleId);
            if (dept > 0 && dept_tag > 0 && user_tag > 0 && role_tag > 0) {
                return 1;
            } else {
                return 0;
            }
        } catch (Exception e) {
            System.out.println("error:" + e.getMessage());
            return 0;
        }
    }
}
